package com.wyb.controller;

import com.wyb.mapper.CommentMapper;
import com.wyb.pojo.Comment;
import com.wyb.constant.Result;
import com.wyb.service.CommentService;
import com.wyb.service.GetReplyService;
import com.wyb.service.ReadAllService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentService commentService;
    @Autowired
    private GetReplyService getReplyService;
    @Autowired
    private ReadAllService readAllService;
    //新增评论
    @PostMapping("/addcomment")
    public Result addComment(@RequestBody Comment comment){

        int insert = commentMapper.insert(comment);

        return insert!=0 ? new Result(200, "评论提交成功", null)
                         : new Result(201, "评论提交失败", null);
    }
    //获得文章评论
    @GetMapping("/getcomment")
    public Result getComment(@RequestParam int topicId){

        return commentService.getComment(topicId);
    }
    //删除评论
    @GetMapping("/deletecomment")
    public Result deleteComment(@RequestParam int id){

        return commentMapper.deleteById(id)!= 0 ? new Result(200, "删除评论成功", null)
                                                : new Result(201, "删除评论失败", null);
    }
    //获得特定用户的所有评论和回复
    @GetMapping("/getreply")
    public Result getReply(@RequestParam int userId){

        return getReplyService.getReply(userId);
    }
    //标记已读
    @GetMapping("/updatereply")
    public Result updateReply(@RequestParam int id){

        return commentMapper.updateReply(id) != 0 ? new Result(200, "成功标记已读", null)
                                                  : new Result(201, "标记已读失败", null);
    }
    //标记全部回复已读
    @GetMapping("/readall")
    public Result readAll(@RequestParam int userId){
        return readAllService.isReadAll(userId);
    }

    //获取评论回复
    @GetMapping("/getreplys")
    public Result getReplys(@RequestParam int id){

        return commentService.getReply(id);
    }
}
